<?php

/**
 *  添加小工具
 */
if (function_exists('register_sidebar')) {
	register_sidebar(array('name' => '全站右侧小工具', 'id' => 'sidebar_global', 'description' => '显示在全站'));
	register_sidebar(array('name' => '首页右侧小工具', 'id' => 'sidebar_home', 'description' => '显示在首页'));
	register_sidebar(array('name' => '正文右侧小工具', 'id' => 'sidebar_single', 'description' => '显示在正文、页面'));
	register_sidebar(array('name' => '归档右侧小工具', 'id' => 'sidebar_article', 'description' => '显示在归档页、搜索、404页'));
}

//~ 文本小工具
add_action('widgets_init', create_function('', 'register_widget( "sidebar_text" );'));
class sidebar_text extends WP_Widget {
	function sidebar_text() {
		$this -> WP_Widget('sidebar_text', '主题 文本小工具', array('description' => '主题自带的文本小工具'));
	}

	function form($instance) {
		$instance = wp_parse_args($instance, array('title' => '文本小工具', 'fonts' => 'lianjie', 'padding' => 10));

		$title = $instance['title'];
		$fonts = $instance['fonts'];
		$padding = $instance['padding'];
		$info = $instance['info'];

		echo '<p>列表标题：<input class="postform" placeholder="自定义标题" id="' . $this -> get_field_id('title') . '"name="' . $this -> get_field_name('title') . '" type="text" value="' . $title . '"/></p>';
		echo '<p>标题图标：<input class="postform" placeholder="自定义标题图标" id="' . $this -> get_field_id('fonts') . '"name="' . $this -> get_field_name('fonts') . '" type="text" value="' . $fonts . '"/></p>';
		echo '<p>内部边距：<select class="postform" id="' . $this -> get_field_id("padding") . '" name="' . $this -> get_field_name("padding") . '">';
		for ($padding = 0; $padding < 21; $padding++)
			echo '<option ' . selected($instance["padding"], $padding) . ' value="' . $padding . '">' . $padding . 'px</option>';
		echo '</select></p>';
		echo '<p>具体内容：<textarea class="widefat" rows="5" id="' . $this -> get_field_id('info') . '" name="' . $this -> get_field_name('info') . '">' . $info . '</textarea></p>';
	}

	function widget($args, $instance) {
		extract($args);
		$title = apply_filters('widget_text', $instance['title']);
		$fonts = apply_filters('widget_text', $instance['fonts']);
		$padding = apply_filters('widget_text', $instance['padding']);
		$info = apply_filters('widget_text', $instance['info']);
		$output = '<div class="sidebar_text">';
		$output .= '<div class="title"><h2><i class="icon icon-' . $fonts . '"></i> ' . $title . '</h2></div>';
		$output .= '<div class="info" style="padding:' . $padding . 'px">' . $info . '</div>';
		$output .= '</div>';
		echo $output;
	}

}

//~ 文章列表
add_action('widgets_init', create_function('', 'register_widget( "sidebar_list" );'));
class sidebar_list extends WP_Widget {
	function sidebar_list() {
		$this -> WP_Widget('sidebar_list', '主题 文章列表', array('description' => '主题自带的文章列表小工具'));
	}

	function form($instance) {
		$instance = wp_parse_args($instance, array('fonts' => 'lianjie', 'number' => 5, 'sort' => 'date'));
		$sort = array( array('title' => '最新发布', 'value' => 'date'), array('title' => '最新评论', 'value' => 'comment'), array('title' => '热门浏览', 'value' => 'views'), array('title' => '大家喜欢', 'value' => 'like'), array('title' => '下载排行', 'value' => 'download'), array('title' => '热评文章', 'value' => 'comment_count'), array('title' => '最新修改', 'value' => 'modified'), array('title' => '随机文章', 'value' => 'rand'));
		$list = array( array('title' => '图文列表', 'value' => 'tuwen'), array('title' => '标题列表', 'value' => 'title'));
		$title = $instance['title'];
		$fonts = $instance['fonts'];
		$number = $instance['number'];

		$output = '<p>列表标题：<input class="postform" placeholder="自定义标题" id="' . $this -> get_field_id('title') . '"name="' . $this -> get_field_name('title') . '" type="text" value="' . $title . '"/></p>';
		$output .= '<p>标题图标：<input class="postform" placeholder="自定义标题图标" id="' . $this -> get_field_id('fonts') . '"name="' . $this -> get_field_name('fonts') . '" type="text" value="' . $fonts . '"/></p>';
		$output .= '<p>显示数量：<input class="postform" placeholder="自定义数量" id="' . $this -> get_field_id('number') . '" name="' . $this -> get_field_name('number') . '" type="text" value="' . $number . '"/></p>';
		$output .= '<style>.postform{width: 158px;height: 27px;resize:none;}</style>';
		echo $output;
		echo '<p>排序方式：<select class="postform" id="' . $this -> get_field_id("sort") . '" name="' . $this -> get_field_name("sort") . '">';
		foreach ($sort as $value)
			echo '<option ' . selected($instance["sort"], $value['value']) . ' value="' . $value['value'] . '">' . $value['title'] . '</option>';
		echo '</select></p>';
		echo '<p>列表风格：<select class="postform" id="' . $this -> get_field_id("list") . '" name="' . $this -> get_field_name("list") . '">';
		foreach ($list as $value)
			echo '<option ' . selected($instance["list"], $value['value']) . ' value="' . $value['value'] . '">' . $value['title'] . '</option>';
		echo '</select></p>';
	}

	function widget($args, $instance) {
		extract($args);
		$title1 = apply_filters('widget_text', $instance['title']);
		$fonts = apply_filters('widget_text', $instance['fonts']);
		$number = apply_filters('widget_text', $instance['number']);
		$sort = apply_filters('widget_text', $instance['sort']);
		$list = apply_filters('widget_text', $instance['list']);
		switch ($sort) {
			case 'rand' :
				$fonts = 'lianjie';
				$titled = '随机文章';
				break;
			case 'views' :
				$fonts = 'lianjie';
				$titled = '热门浏览';
				$sort = 'meta_value_num';
				$key = array( array('key' => 'views'));
				break;
			case 'like' :
				$fonts = 'lianjie';
				$titled = '大家喜欢';
				$sort = 'meta_value_num';
				$key = array( array('key' => 'like'));
				break;
			case 'download' :
				$fonts = 'lianjie';
				$titled = '下载排行';
				$sort = 'meta_value_num';
				$key = array( array('key' => 'download'));
				break;
			case 'comment' :
				$fonts = 'lianjie';
				$titled = '最新评论';
				$sort = 'meta_value_num';
				$key = array( array('key' => 'comment'));
				$comment = 'comment';
				break;
			case 'modified' :
				$fonts = 'lianjie';
				$titled = '最新修改';
				break;
			case 'comment_count' :
				$fonts = 'lianjie';
				$titled = '热评文章';
				break;
			default :
				$fonts = 'lianjie';
				$titled = '最新发布';
				break;
		}

		if (!empty($title1))
			$titled = $title1;
		$title = '<div class="title"><h2><i class="icon icon-' . $fonts . '"></i> ' . $titled . '</h2></div>';

		if ($list == 'title') {
			sidebar_title($title, $key, $sort, $number);
		} else {
			if ($comment == 'comment')
				sidebar_comment($title, $key, $sort, $number);
			else
				sidebar_list($title, $key, $sort, $number);

		}
	}

}

//~ 分类列表
add_action('widgets_init', create_function('', 'register_widget( "sidebar_cat" );'));
class sidebar_cat extends WP_Widget {
	function sidebar_cat() {
		$this -> WP_Widget('sidebar_cat', '主题 分类列表', array('description' => '主题自带的分类列表小工具'));
	}

	function form($instance) {
		$instance = wp_parse_args($instance, array('fonts' => 'lianjie', 'number' => 5));
		$list = array( array('title' => '图文列表', 'value' => 'tuwen'), array('title' => '标题列表', 'value' => 'title'));
		$fonts = $instance['fonts'];
		$number = $instance['number'];
		$cat = $instance['cat'];
		$output .= '<p>标题图标：<input class="postform" placeholder="自定义标题图标" id="' . $this -> get_field_id('fonts') . '"name="' . $this -> get_field_name('fonts') . '" type="text" value="' . $fonts . '"/></p>';
		$output .= '<p>显示数量：<input class="postform" placeholder="自定义数量" id="' . $this -> get_field_id('number') . '" name="' . $this -> get_field_name('number') . '" type="text" value="' . $number . '"/></p>';
		echo $output;
		echo '<p>文章分类：';
		wp_dropdown_categories(array('hierarchical' => 1, 'show_count' => 1, 'hide_empty' => 0, 'class' => 'postform', 'name' => $this -> get_field_name("cat"), 'selected' => $instance["cat"]));
		echo '</p>';
		echo '<p>列表风格：<select class="postform" id="' . $this -> get_field_id("list") . '" name="' . $this -> get_field_name("list") . '">';
		foreach ($list as $value)
			echo '<option ' . selected($instance["list"], $value['value']) . ' value="' . $value['value'] . '">' . $value['title'] . '</option>';
		echo '</select></p>';
	}

	function widget($args, $instance) {
		extract($args);
		$fonts = apply_filters('widget_text', $instance['fonts']);
		$number = apply_filters('widget_text', $instance['number']);
		$cat = apply_filters('widget_text', $instance['cat']);
		$list = apply_filters('widget_text', $instance['list']);

		$title = '<div class="title"><h2><i class="icon icon-' . $fonts . '"></i> <a target="_blank" href="' . get_category_link($cat) . '">' . get_cat_name($cat) . '</a></h2></div>';

		sidebar_cat($title, $cat, $number);

	}

}
?>
